﻿using AmazonMODEL;
using MODEL;
using SysHelper;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SysController
{
    //Feeds
    public static partial class SystemController
    {
        #region 2015-11-06 本机允许运行的帐号 + List<AppKey> RunAccount
        private static List<AppKey> _RunAccount = null;
        /// <summary>
        /// 2015-11-06 本机允许运行的帐号
        /// </summary>
        public static List<AppKey> RunAccount
        {
            get
            {
                if (_RunAccount == null)
                {
                    string HostName = System.Net.Dns.GetHostName();
                    _RunAccount = OperateContext.CurrentContext.BllSession.IAppKey_BLL.GetListByWhere(s => s.RunServices == HostName);
                }
                return _RunAccount;
            }
        }
        #endregion

        #region  2016-11-06 查询任务发布信息 + static List<AdRelease> GetAdRelease(int AccountInfoId, ApiOperationType type)
        /// <summary>
        /// 2016-11-06 查询任务发布信息
        /// </summary>
        /// <param name="AccountInfoId">帐号ID</param>
        /// <param name="type">操作类型</param> 
        /// <returns></returns>
        public static List<AdRelease> GetAdRelease(int AccountInfoId, ApiOperationType type)
        {
            List<AdRelease> listAdRelease = null;
            switch (type)
            {
                case ApiOperationType.Product:
                    listAdRelease = OperateContext.CurrentContext.BllSession.IAdRelease_BLL.GetListByWhere(s => s.AccountInfoId == AccountInfoId
                         && s.IsUpdateProduct == true
                         && s.IsDel == false
                         && s.StartTime <= DateTime.Now, s => s.Levels, false);
                    break;
                case ApiOperationType.Relationship:
                    listAdRelease = OperateContext.CurrentContext.BllSession.IAdRelease_BLL.GetListByWhere(s => s.AccountInfoId == AccountInfoId
                        && s.IsUpdateRelationship == true
                        && s.IsRelease == false
                        && s.IsDel == false
                        && s.StartTime <= DateTime.Now, s => s.Levels, false);
                    break;
                case ApiOperationType.Price:
                    listAdRelease = OperateContext.CurrentContext.BllSession.IAdRelease_BLL.GetListByWhere(s => s.AccountInfoId == AccountInfoId
                       && s.IsUpdatePrice == true
                       && s.IsRelease == false
                       && s.IsDel == false
                       && s.StartTime <= DateTime.Now, s => s.Levels, false);
                    break;
                case ApiOperationType.Image:
                    listAdRelease = OperateContext.CurrentContext.BllSession.IAdRelease_BLL.GetListByWhere(s => s.AccountInfoId == AccountInfoId
                      && s.IsUpdateImage == true
                      && s.IsRelease == false
                      && s.IsDel == false
                      && s.StartTime <= DateTime.Now, s => s.Levels, false);
                    break;
                case ApiOperationType.Inventory:
                    listAdRelease = OperateContext.CurrentContext.BllSession.IAdRelease_BLL.GetListByWhere(s => s.AccountInfoId == AccountInfoId
                      && s.IsUpdateInventory == true
                      && s.IsRelease == false
                      && s.IsDel == false
                      && s.StartTime <= DateTime.Now, s => s.Levels, false);
                    break;
            }
            return listAdRelease;
        }
        #endregion

        #region 2016-11-06 查询任务发布信息 + static List<AdRelease> GetAdRelease(List<string> AdReleaseIdList)
        /// <summary>
        /// 2016-11-06 查询任务发布信息
        /// </summary> 
        public static List<AdRelease> GetAdRelease(List<string> idList)
        {
            List<int> list = new List<int>();
            foreach (var item in idList)
            {
                list.Add(Convert.ToInt32(item));
            }
            return OperateContext.CurrentContext.BllSession.IAdRelease_BLL.GetListByWhere(s => list.Contains(s.AdReleaseID));
        }
        #endregion

        #region  2016-11-06 查询任务发布信息 +  static List<AdRelease> GetAdRelease(int AccountId, string[] apiOperationStatusArr)
        /// <summary>
        /// 2016-11-06 查询任务发布信息
        /// </summary>
        /// <param name="AccountId"></param>
        /// <param name="apiOperationStatusArr"></param>
        /// <returns></returns>
        public static List<AdRelease> GetAdRelease(int AccountId, string[] apiOperationStatusArr)
        {
            return OperateContext.CurrentContext.BllSession.IAdRelease_BLL.GetListByWhere(s => s.AccountInfoId == AccountId && apiOperationStatusArr.Contains(s.ApiOperationStatus));
        }
        #endregion

        #region 2015-11-05 修改AdRelease + static int ModifyAdRelease(List<AdRelease> list)
        /// <summary>
        /// 2015-11-05 修改AdRelease
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int ModifyAdRelease(List<AdRelease> list)
        {
            if (list == null || list.Count < 0)
            {
                return 0;
            }
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.IAdRelease_BLL.ModifyIsNullADD(item, s => s.AdReleaseID == item.AdReleaseID, true);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(AdRelease).Name);

            }
            catch (Exception)
            {
                return -1;
            }
        }
        #endregion

        #region 2015-11-06 添加发布任务 +  static int AddFeedsInfo(FeedsInfo model)
        /// <summary>
        /// 2015-11-06 添加发布任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int AddFeedsInfo(FeedsInfo model)
        {
            try
            {
                OperateContext.CurrentContext.BllSession.IFeedsInfo_BLL.ADD(model);
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(FeedsInfo).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        }
        #endregion

        #region 2015-11-05 查询FeedsInfo + static List<FeedsInfo> GetFeedsInfo(int AccountInfoId, FeedProcessingStatus status, int rows)
        /// <summary>
        ///  2015-11-05 查询FeedsInfo
        /// </summary>
        /// <param name="AccountInfoId">店铺ID</param>
        /// <param name="status">操作状态</param>
        /// <param name="rows">查询多少行</param>
        /// <returns></returns>
        public static List<FeedsInfo> GetFeedsInfo(int AccountInfoId, FeedProcessingStatus status, int rows)
        {
            string strStatus = status.ToString();
            return OperateContext.CurrentContext.BllSession.IFeedsInfo_BLL.GetListByWhere(s => s.AccountInfoId == AccountInfoId
                && s.FeedProcessingStatus == strStatus, s => s.CreateTime, false).Take(rows).ToList();
        }
        #endregion

        #region 2015-11-05 修改FeedsInfo + static int ModifyFeedsInfo(List<FeedsInfo> list)
        /// <summary>
        /// 2015-11-05 修改 FeedsInfo 
        /// </summary>
        /// <param name="list">发布任务</param>
        /// <param name="type">操作</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public static int ModifyFeedsInfo(List<FeedsInfo> list)
        {
            if (list == null || list.Count < 1)
                return 0;
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.IFeedsInfo_BLL.ModifyIsNullADD(item, s => s.FeedsInfoID == item.FeedsInfoID, true);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(FeedsInfo).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        }
        #endregion

    }

    //Reports
    public static partial class SystemController
    {
        #region 2015-11-03 添加 ReportsInfo + static int AddReportsInfo(ReportsInfo model)
        /// <summary>
        /// 2015-11-03 添加 ReportsInfo
        /// </summary>
        /// <param name="model">ReportsInfo</param>
        /// <returns></returns>
        public static int AddReportsInfo(ReportsInfo model)
        {
            try
            {
                OperateContext.CurrentContext.BllSession.IReportsInfo_BLL.ADD(model);
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(ReportsInfo).Name);
            }
            catch (Exception)
            {
                return -1;
            }

        }
        #endregion

        #region 2015-11-03 批量添加 ReportsInfo + static int AddReportsInfo(List<ReportsInfo> list)
        /// <summary>
        /// 2015-11-03 添加 ReportsInfo
        /// </summary>
        /// <param name="list">ReportsInfo集</param>
        /// <returns></returns>
        public static int AddReportsInfo(List<ReportsInfo> list)
        {
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.IReportsInfo_BLL.ADD(item);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(ReportsInfo).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        }

        #endregion

        #region 2015-11-03 查询ReportsInfo   +  static List<ReportsInfo> GetReportsInfo(int AccountInfoId, ReportProcessingStatus status)
        /// <summary>
        /// 2015-11-03 查询ReportsInfo   
        /// </summary>
        /// <param name="AccountInfoId">店铺ID</param>
        /// <returns></returns>
        public static List<ReportsInfo> GetReportsInfo(int AccountInfoId, ReportProcessingStatus status)
        {
            string strStatus = status.ToString();
            return OperateContext.CurrentContext.BllSession.IReportsInfo_BLL.GetListByWhere(s => s.AccountInfoId == AccountInfoId && s.ReportProcessingStatus == strStatus && s.IsDel == false,
                s => s.CreateTime, false);
        }
        #endregion

        #region 2015-11-03 修改ReportsInfo + static int ModifyReportsInfo(List<ReportsInfo> list)
        /// <summary>
        /// 2015-11-03 修改ReportsInfo 
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int ModifyReportsInfo(List<ReportsInfo> list)
        {
            if (list == null || list.Count < 1)
                return 0;
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.IReportsInfo_BLL.ModifyIsNullADD(item, s => s.ReportsInfoID == item.ReportsInfoID, true);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(ReportsInfo).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        }
        #endregion

        #region  2015-11-10 添加 SellerDetailsListing  + static int AddSellerDetailsListing(List<SellerDetailsListing> list)
        /// <summary>
        /// 2015-11-10 添加 SellerDetailsListing 
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int AddSellerDetailsListing(List<SellerDetailsListing> list)
        {
            if (list.Count < 1 || list == null)
            {
                return 0;
            }
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.ISellerDetailsListing_BLL.ModifyIsNullADD(item, s => s.AccountInfoId == item.AccountInfoId
                        && s.seller_sku == item.seller_sku, true);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(SellerDetailsListing).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        }
        #endregion

        #region 2015-11-11 添加 SellerListing + static int AddSellerListing(List<SellerListing> list)
        /// <summary>
        /// 2015-11-11 添加 SellerListing
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int AddSellerListing(List<SellerListing> list)
        {
            if (list.Count < 1 || list == null)
            {
                return 0;
            }
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.ISellerListing_BLL.ModifyIsNullADD(item, s => s.AccountInfoId == item.AccountInfoId && s.SKU == item.SKU, true);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(SellerListing).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        }
        #endregion

        #region 2015-11-17 添加 AmazonBrowseNode + static int AddAmazonBrowseNode(List<AmazonBrowseNode> list)
        /// <summary>
        /// 2015-11-17 添加 AmazonBrowseNode
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int AddAmazonBrowseNode(List<AmazonBrowseNode> list)
        {
            if (list.Count < 1 || list == null)
            {
                return 0;
            }
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.IAmazonBrowseNode_BLL.ADD(item);//, s => s.ProductTypeDefinitions == item.ProductTypeDefinitions && s.BrowseNodeId == item.BrowseNodeId);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(AmazonBrowseNode).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        } 
        #endregion

        #region 2015-11-17 添加 AmazonRefinementsInfo +  static int AddAmazonRefinementsInfo(List<AmazonRefinementsInfo> list)
        /// <summary>
        /// 2015-11-17 添加 AmazonRefinementsInfo
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int AddAmazonRefinementsInfo(List<AmazonRefinementsInfo> list)
        {
            if (list.Count < 1 || list == null)
            {
                return 0;
            }
            try
            {
                foreach (var item in list)
                {
                     
                    OperateContext.CurrentContext.BllSession.IAmazonRefinementsInfo_BLL.ADD(item);//, s => s.RefinementName == item.RefinementName && s.BrowseNodeId == item.BrowseNodeId);
                     

                } 
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(AmazonRefinementsInfo).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        } 
        #endregion
    }

    //Orders
    public static partial class SystemController
    {
        #region 2015-11-08 添加 AmazonOrders + static int AddAmazonOrders(List<AmazonOrder> list)
        /// <summary>
        /// 2015-11-08 添加 AmazonOrders
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int AddAmazonOrders(List<AmazonOrder> list)
        {
            if (list == null || list.Count < 1)
                return 0;
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.IAmazonOrder_BLL.ModifyIsNullADD(item,
                        s => s.AmazonOrderId == item.AmazonOrderId && s.AccountInfoId == item.AccountInfoId, true);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(AmazonOrder).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        } 
        #endregion

        #region 2015-11-08 添加 AmazonOrdersShippingAddress + static int AddAmazonOrdersShippingAddress(List<AmazonOrdersShippingAddress> list)
        /// <summary>
        /// 2015-11-08 添加 AmazonOrdersShippingAddress
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int AddAmazonOrdersShippingAddress(List<AmazonOrdersShippingAddress> list)
        {
            if (list == null || list.Count < 1)
                return 0;
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.IAmazonOrdersShippingAddress_BLL.ModifyIsNullADD(item,
                        s => s.AmazonOrderId == item.AmazonOrderId && s.AccountInfoId == item.AccountInfoId, true);
                    AmazonOrderFulfillment model = new AmazonOrderFulfillment()
                    {
                        AccountInfoId = item.AccountInfoId,
                        AmazonOrderID = item.AmazonOrderId,
                        IsDel = false,
                        SiteId = item.SiteId,
                        CreateTime = DateTime.Now,
                        CreateUser = "OrderFulfillment",
                        UpdateTime = DateTime.Now,
                        UpdateUser = "Fulfillment",
                        CarrierName = "USPS",
                        ShippingMethod = "Standard",
                        FulfillmentDate = DateTime.Now.AddHours(8)
                    };
                    OperateContext.CurrentContext.BllSession.IAmazonOrderFulfillment_BLL.ModifyIsNullADD(model, s => s.AccountInfoId == model.AccountInfoId && s.AmazonOrderID == model.AmazonOrderID && s.SiteId == model.SiteId, true);
                }
                OperateContext.CurrentContext.BllSession.SaveChanges(typeof(AmazonOrderFulfillment).Name);
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(AmazonOrdersShippingAddress).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        } 
        #endregion

        #region  2015-11-08 添加 AmazonOrdersTotal +  static int AddAmazonOrdersTotal(List<AmazonOrdersTotal> list)
        /// <summary>
        /// 2015-11-08 添加 AmazonOrdersTotal
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int AddAmazonOrdersTotal(List<AmazonOrdersTotal> list)
        {
            if (list == null || list.Count < 1)
                return 0;
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.IAmazonOrdersTotal_BLL.ModifyIsNullADD(item,
                        s => s.AmazonOrderId == item.AmazonOrderId && s.AccountInfoId == item.AccountInfoId, true);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(AmazonOrdersTotal).Name);
            }
            catch (Exception)
            {
                return -1;
            }
        } 
        #endregion

        #region 2015-11-08 查询AmazonOrders +  static List<AmazonOrder> GetAmazonOrders(AppKey appkey)
        /// <summary>
        /// 2015-11-08 查询AmazonOrders
        /// </summary>
        /// <param name="appkey"></param>
        /// <returns></returns>
        public static List<AmazonOrder> GetAmazonOrders(AppKey appkey)
        {
            return OperateContext.CurrentContext.BllSession.IAmazonOrder_BLL.GetListByWhere(s => s.IsDetails == false && s.IsDel == false);
        } 
        #endregion

        #region  2015-11-08 添加 AmazonOrdersDetails + static int AddAmazonOrdersDetails(List<AmazonOrdersDetail> list)
        /// <summary>
        /// 2015-11-08 添加 AmazonOrdersDetails
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int AddAmazonOrdersDetails(List<AmazonOrdersDetail> list)
        {
            foreach (var item in list)
            {
                AmazonOrderFulfillmentItem model = new AmazonOrderFulfillmentItem()
                  {
                      AccountInfoId = item.AccountInfoId,
                      AmazonOrderID = item.AmazonOrderId,
                      IsShiped = -1,
                      AmazonOrderItemID = item.OrderItemId,
                      CreateTime = DateTime.Now,
                      CreateUser = "ItemDetails",
                      OrderQty = Convert.ToInt16(item.QuantityOrdered),
                      IsDel = false,
                      SiteId = item.SiteId,
                      UpdateTime = DateTime.Now,
                      UpdateUser = "Item"
                  };
                OperateContext.CurrentContext.BllSession.IAmazonOrderFulfillmentItem_BLL.ModifyIsNullADD(model, s => s.AccountInfoId == model.AccountInfoId &&
                    s.SiteId == model.SiteId && s.AmazonOrderID == model.AmazonOrderID && s.AmazonOrderItemID == model.AmazonOrderItemID,true);

                OperateContext.CurrentContext.BllSession.IAmazonOrdersDetail_BLL.ModifyIsNullADD(item, s => s.AccountInfoId == item.AccountInfoId && s.OrderItemId == item.OrderItemId, true);
            }
            OperateContext.CurrentContext.BllSession.SaveChanges(typeof(AmazonOrderFulfillmentItem).Name);
            return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(AmazonOrdersDetail).Name);
        }
        #endregion

        #region 2015-11-08 修改 AmazonOrders + static int ModifyAmazonOrders(List<AmazonOrder> list)
        /// <summary>
        /// 2015-11-08 修改 AmazonOrders
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int ModifyAmazonOrders(List<AmazonOrder> list)
        {
            if (list.Count < 1 || list == null)
            {
                return 0;
            }
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.IAmazonOrder_BLL.ModifyIsNullADD(item, s => s.AmazonOrdersID == item.AmazonOrdersID, true);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(AmazonOrder).Name);
            }
            catch (Exception)
            {
                return -1;
            }

        } 
        #endregion

        #region 2015-11-18 查询 AamzonOrderFilfillment +  static List<AmazonOrderFulfillment> GetAmazonOrderFulfillment(AppKey appkey)
        /// <summary>
        /// 2015-11-18 查询 AamzonOrderFilfillment
        /// </summary>
        /// <param name="appkey"></param>
        /// <returns></returns>
        public static List<AmazonOrderFulfillment> GetAmazonOrderFulfillment(AppKey appkey)
        {
            return OperateContext.CurrentContext.BllSession.IAmazonOrderFulfillment_BLL.GetListByWhere(s => s.AccountInfoId == appkey.AccountInfoID && s.IsDel == false);
        } 
        #endregion

        #region 2015-11-18 查询寄送订单Item + static List<AmazonOrderFulfillmentItem> GetAmazonOrderFulfillmentItem(List<AmazonOrderFulfillment> list)
        /// <summary>
        /// 2015-11-18 查询寄送订单Item
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<AmazonOrderFulfillmentItem> GetAmazonOrderFulfillmentItem(List<AmazonOrderFulfillment> list)
        {
            List<string> itemList = list.Select(s => s.AmazonOrderID).ToList();
            return OperateContext.CurrentContext.BllSession.IAmazonOrderFulfillmentItem_BLL.GetListByWhere(s => itemList.Contains(s.AmazonOrderID));
        } 
        #endregion

    }

    //Products
    public static partial class SystemController
    {
        #region 2015-11-11 查询  SellerListing (在售数据) + static List<SellerListing> GetSellerListing(int AccountInfoId)
        /// <summary>
        /// 2015-11-11 查询  SellerListing (在售数据)
        /// </summary>
        /// <param name="appkey"></param>
        /// <returns></returns>
        public static List<SellerListing> GetSellerListing(int AccountInfoId)
        {
            return OperateContext.CurrentContext.BllSession.ISellerListing_BLL.GetListByWhere(s => s.AccountInfoId == AccountInfoId
                && s.Price > 0.00m
                && s.Quantity > 0
                && s.IsDel == false);
        }
        #endregion

        #region 2015-11-15 添加 CompetitivePrice +  static int AddCompetitivePrice(List<CompetitivePrice> list)
        /// <summary>
        /// 2015-11-15 添加 CompetitivePrice 
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int AddCompetitivePrice(List<CompetitivePrice> list)
        {
            if (list.Count < 1 || list == null)
            {
                return 0;
            }
            bool IsAgain = true;
        Again:
            try
            {
                foreach (var item in list)
                {
                    OperateContext.CurrentContext.BllSession.ICompetitivePrice_BLL.ModifyIsNullADD(item, s => s.AccountInfoId == item.AccountInfoId
                        && s.ASIN == item.ASIN && s.MarketplaceCode == item.MarketplaceCode, true);
                }
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(CompetitivePrice).Name);
            }
            catch (Exception ex)
            {
                if (IsAgain)
                {
                    IsAgain = false;
                    SysCommon.Log.Log.Write ("修改CompetivtivePrice 异常，重试");
                    goto Again;
                }
                SysCommon.Log.Log.WriteError(ex, "修改CompetivtivePrice 异常");
                return -1;
            }
        } 
        #endregion

        #region 2015-11-15 添加 SalesRank + static int AddSalesRank(SalesRank model)
        /// <summary>
        /// 2015-11-15 添加 SalesRank
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int AddSalesRank(SalesRank model)
        {
            if (model == null)
            {
                return 0;
            }
            bool IsAgain = true;
        Again:
            try
            {
                OperateContext.CurrentContext.BllSession.ISalesRank_BLL.ModifyIsNullADD(model, s => s.AccountInfoId == model.AccountInfoId 
                    && s.ASIN == model.ASIN && s.MarketplaceCode == model.MarketplaceCode,true);
                return OperateContext.CurrentContext.BllSession.SaveChanges(typeof(SalesRank).Name);
            }
            catch (Exception ex)
            {
                if (IsAgain)
                {
                    IsAgain = false;
                    SysCommon.Log.Log.Write("修改SalesRan 异常，重试");
                    goto Again;
                }
                SysCommon.Log.Log.WriteError(ex, "修改SalesRan 异常");
                return -1;
            }
        } 
        #endregion
    }
}
